package com.amsp.oa.oademo.service.impl;

import com.amsp.oa.oademo.entity.DocumentRevisionApply;
import com.amsp.oa.oademo.entity.DocumentRevisionReview;
import com.amsp.oa.oademo.mapper.DocumentRevisionApplyMapper;
import com.amsp.oa.oademo.mapper.DocumentRevisionReviewMapper;
import com.amsp.oa.oademo.service.DocumentRevisionService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
public class DocumentRevisionServiceImpl extends ServiceImpl<DocumentRevisionApplyMapper, DocumentRevisionApply> implements DocumentRevisionService {

    @Autowired
    private DocumentRevisionReviewMapper reviewMapper;

    @Override
    public IPage<DocumentRevisionApply> getPage(Integer current, Integer size, String fileName, String fileNumber) {
        Page<DocumentRevisionApply> page = new Page<>(current, size);
        LambdaQueryWrapper<DocumentRevisionApply> wrapper = new LambdaQueryWrapper<>();
        
        if (StringUtils.hasText(fileName)) {
            wrapper.like(DocumentRevisionApply::getFileName, fileName);
        }
        if (StringUtils.hasText(fileNumber)) {
            wrapper.like(DocumentRevisionApply::getFileNumber, fileNumber);
        }
        
        wrapper.orderByDesc(DocumentRevisionApply::getCreateTime);
        return this.page(page, wrapper);
    }

    @Override
    public DocumentRevisionApply getDetailById(Long id) {
        DocumentRevisionApply apply = this.getById(id);
        if (apply != null) {
            LambdaQueryWrapper<DocumentRevisionReview> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(DocumentRevisionReview::getApplyId, id);
            List<DocumentRevisionReview> reviews = reviewMapper.selectList(wrapper);
            apply.setReviewList(reviews);
        }
        return apply;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveOrUpdateApply(DocumentRevisionApply apply) {
        boolean result = this.saveOrUpdate(apply);
        if (result && apply.getReviewList() != null) {
            for (DocumentRevisionReview review : apply.getReviewList()) {
                review.setApplyId(apply.getId());
                if (review.getId() != null) {
                    reviewMapper.updateById(review);
                } else {
                    reviewMapper.insert(review);
                }
            }
        }
        return result;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteById(Long id) {
        // 删除主表数据
        boolean result = this.removeById(id);
        if (result) {
            // 删除关联的审批意见
            LambdaQueryWrapper<DocumentRevisionReview> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(DocumentRevisionReview::getApplyId, id);
            reviewMapper.delete(wrapper);
        }
        return result;
    }
} 